package com.awesome.javademo.threads.thread;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.awesome.common.util.log.ShowLogUtil;
import com.awesome.javademo.R;

public class ThreadLocalActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_local);
        threadLocalTest();
    }

    static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    public static void threadLocalTest() {
        new Thread(){
            @Override
            public void run() {
                threadLocal.set("第一个线程");
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info(Thread.currentThread().getName()+"-----"+threadLocal.get());
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                threadLocal.set("第二个线程");
                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info(Thread.currentThread().getName()+"-----"+threadLocal.get());

            }
        }.start();
        threadLocal.set("main线程");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ShowLogUtil.info(Thread.currentThread().getName()+"-----"+threadLocal.get());
    }
}
